package com.shi.yibanjia.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.shi.yibanjia.entity.dto.RoleEditDto;
import com.shi.yibanjia.entity.po.Account2Role;
import com.shi.yibanjia.entity.po.Role;
import com.shi.yibanjia.entity.po.Role2Menu;
import com.shi.yibanjia.exception.BusinessException;
import com.shi.yibanjia.mapper.RoleMapper;
import com.shi.yibanjia.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2024-05-16 17:20:29
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public boolean add(Role role) {
        Long count = lambdaQuery().eq(Role::getRoleCode, role.getRoleCode())
                .or().eq(Role::getRoleName, role.getRoleName()).count();
        if (count != null && count > 0) {
            throw new BusinessException("该角色Code或Name已存在");
        }
        return baseMapper.insert(role) > 0;
    }

    /**
     * 修改数据
     *
     * @param roleEditDto 实例对象
     * @return 是否成功
     */
    @Transactional
    @Override
    public void giveMenu(RoleEditDto roleEditDto) {
        // 1. 删除该角色原有的权限
        Db.lambdaUpdate(Role2Menu.class)
                .eq(Role2Menu::getRoleId, roleEditDto.getRoleId())
                .remove();
        // 2. 添加该角色的新权限
        List<Integer> fullCheck = roleEditDto.getFullCheck();
        List<Integer> halfCheck = roleEditDto.getHalfCheck();
        List<Role2Menu> fullMenu = fullCheck.stream().map(item -> {
            Role2Menu role2Menu = new Role2Menu();
            role2Menu.setMenuId(item);
            role2Menu.setRoleId(roleEditDto.getRoleId());
            role2Menu.setFullCheck(1);
            return role2Menu;
        }).collect(Collectors.toList());
        List<Role2Menu> halfMenu = halfCheck.stream().map(item -> {
            Role2Menu role2Menu = new Role2Menu();
            role2Menu.setMenuId(item);
            role2Menu.setRoleId(roleEditDto.getRoleId());
            role2Menu.setFullCheck(0);
            return role2Menu;
        }).toList();
        fullMenu.addAll(halfMenu);
        if (!fullMenu.isEmpty()) {
            Db.saveBatch(fullMenu);
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Transactional
    @Override
    public void removeById(Integer roleId) {
        // 1. 删除角色
        int deleted = baseMapper.deleteById(roleId);
        // 2. 删除用户角色信息
        Db.lambdaUpdate(Account2Role.class)
                .eq(Account2Role::getRoleId, roleId)
                .remove();
        // 3. 删除角色菜单信息
        Db.lambdaUpdate(Role2Menu.class)
                .eq(Role2Menu::getRoleId, roleId)
                .remove();
    }

    @Override
    public List<Role> listByCondition(String likeProp) {
        List<Role> list = lambdaQuery()
                .like(Role::getRoleName, likeProp)
                .or()
                .like(Role::getRoleCode, likeProp)
                .orderByDesc(Role::getCreateTime)
                .list();
        return list;
    }

}
